<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout"
      layout:decorate="~{layout/_header.html}">
<body>
<link rel="stylesheet" th:href="'/base/lib/webuploader/upload/upload.css?v='+${staticVersion}">
<link rel="stylesheet" th:href="'/base/lib/jqueryui/css/jquery-ui.css?v='+${staticVersion}">

<article class="page-container">
    <form class="form form-horizontal" id="form-admin-add">
        <input type="hidden" id="id" name="id" value="${data.id}" />
        <div class="row cl">
            <label class="form-label col-sm-1"><span class="c-red">*</span>商品图片：</label>
            <div class="formControls col-xs-8 col-sm-9">
                <h5 style="margin-top: 5px;cursor: text;text-align: left;margin-left:-7px;"><small>（最多可添加5张图片，建议图片尺寸800*800px，大小≤1MB，支持JPG、PNG、JPEG）</small></h5>
            </div>
        </div>
        <div class="row cl">
            <label class="form-label col-sm-1">&nbsp;</label>
            <div class="formControls col-xs-8 col-sm-9">
                <div class="ibox-content">
                    <div class="form-group">
                        <div class="page-container">
                            <div id="uploader" class="wu-example uploader">
                                <div class="queueList">
                                    <ul class="filelist" id="filelist"></ul>
                                    <div class="mainImgText">商品主图</div>
                                    <div id="filePicker" class="placeholder dndArea"></div>
                                </div>
                                <div class="clear"></div>
                                <div class="statusBar" style="display:none;">
                                    <div class="progress">
                                        <span class="text">0%</span>
                                        <span class="percentage"></span>
                                    </div>
                                    <div class="info"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="clearfix"></div>
                </div>
            </div>
            <input type="hidden" value="${data.main_img}" placeholder="主图" id="main_img" name="main_img">
            <input type="hidden" value="${data.other_img}" placeholder="非主图" id="other_img" name="other_img">
        </div>

        <div class="row cl">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                <input class="btn btn-primary radius" type="button" id="btn_submit" value="&nbsp;&nbsp;提交&nbsp;&nbsp;">
            </div>
        </div>
    </form>
</article>

<!-- 配置文件 -->
<!--<script type="text/javascript" src="/base/lib/ueditor/ueditor.config.js"></script>-->
<!-- 编辑器源码文件 -->
<!--<script type="text/javascript" src="/base/lib/ueditor/ueditor.all.js"></script>-->
<!-- 实例化编辑器 -->

<script type="text/javascript" th:src="'/base/lib/webuploader/0.1.5/webuploader.js?v='+${staticVersion}"></script>
<script type="text/javascript" th:src="'/base/lib/webuploader/upload/upload.js?v='+${staticVersion}"></script>
<script type="text/javascript" th:src="'/base/lib/webuploader/upload/jquery.dragsort.min.js?v='+${staticVersion}"></script>
<script type="text/javascript" th:src="'/base/lib/jqueryui/js/jquery-ui.js?v='+${staticVersion}"></script>

<script type="text/javascript">
    var file_images = [];

    /*初始化界面元素*/
    function initial(){
        var id=$("#id").val();
        if(id>0){
            var main_img=$("#main_img").val();
            var other_img=$("#other_img").val();
            if(main_img!=""){
                var mainImage = {
                    'dataUrl': main_img,
                    'imgServerUrl': imgUrl + "/" + main_img
                };
                var imgInfoJson = {};
                imgInfoJson.url = mainImage.imgServerUrl;
                imgInfoJson.id = "id" + 0;
                imgInfoJson.sourceUrl = mainImage.dataUrl;
                file_images.push(imgInfoJson);
            }
            if(other_img!=null){
                var otherImgArray = other_img.split('|');
                for (var i = 0; i < otherImgArray.length; i++) {
                    if (otherImgArray[i].length > 0) {
                        var otherImage = {
                            'dataUrl': otherImgArray[i],
                            'imgServerUrl': imgUrl + "/" + otherImgArray[i]
                        };
                        var imgInfoJson = {};
                        imgInfoJson.url = otherImage.imgServerUrl;
                        imgInfoJson.id = "id" + (i + 1);
                        imgInfoJson.sourceUrl = otherImgArray[i];
                        file_images.push(imgInfoJson);
                    }
                }
            }
        }
        projectUpLoader();

    }

    /*该方法是再拼接div内容时加上的，即鼠标选中的时候，input框输入选中值  */
    function select(id,name){
        $("#b_id").val(id);
        $("#b_name").val(name);
        $(".show_div").hide();
    }
    function selectsupplier(id,name){
        $("#supplier_id").val(id);
        $("#supplier_name").val(name);
        $(".supplier_show_div").hide();
    }


    var file_images = [];
    $(function(){
        // imgInfoJson.url =  otherImage.imgServerUrl;
        // imgInfoJson.id = "id" + (i+1);
        // imgInfoJson.sourceUrl = otherImgArray[i];


        initial();
        /*拖拽顺序*/
        $(".filelist").dragsort({
            dragSelector: ".imgWrap",
            dragEnd: function() { },
            dragBetween: false,
            placeHolderTemplate: "<li></li>"
        });


        /*图片拖拽*/
        $(".goodsImgWrap").dragsort({
            dragSelector: ".goodsImgItem",
            dragEnd: function() { },
            dragBetween: false,
            placeHolderTemplate: "<li></li>"
        });


        $("#btn_submit").click(function () {
            var id=$("#id").val();
            var cat_one_id=$("#cat_one_id").val();
            var cat_two_id=$("#cat_two_id").val();
            var cat_three_id=$("#cat_three_id").val();
            if(cat_three_id==""){
                layer.msg('请选择商品分类!',{icon:1,time:1000});
                return false;
            }
            var cat_one_name=$("#cat_one_id").find("option:selected").text();
            var cat_two_name=$("#cat_two_id").find("option:selected").text();
            var cat_three_name=$("#cat_three_id").find("option:selected").text();

            var name=$("#name").val();
            if(name==""){
                layer.msg('请输入商品名称!',{icon:1,time:1000});
                return false;
            }

            var selling_point=$("#selling_point").val();
            if(selling_point==""){
                layer.msg('请输入卖点!',{icon:1,time:1000});
                return false;
            }
            var b_name=$("#b_name").val();
            if(b_name==""){
                $("#b_id").val("");
            }

            var b_id=$("#b_id").val();
            if(b_id==""){
                layer.msg('请输入品牌!',{icon:1,time:1000});
                return false;
            }


            var cost_price=$("#cost_price").val();
            if(cost_price==""){
                layer.msg('请输入成本价!',{icon:1,time:1000});
                return false;
            }

            var market_price=$("#market_price").val();
            if(market_price=="" || parseFloat(market_price)<=0){
                layer.msg('请输入市场价!',{icon:1,time:1000});
                return false;
            }

            var supplier_name=$("#supplier_name").val();
            if(supplier_name==""){
                $("#supplier_id").val("");
            }

            var supplier_id=$("#supplier_id").val();
            if(supplier_id==""){
                layer.msg('请输入供货商!',{icon:1,time:1000});
                return false;
            }



            var gold_price=$("#gold_price").val();
            if(gold_price=="" || parseFloat(gold_price)<=0){
                layer.msg('请输入金牌会员价!',{icon:1,time:1000});
                return false;
            }

            //var silver_price=$("#silver_price").val();
            var silver_price=gold_price;
            if(silver_price==""|| parseFloat(silver_price)<=0){
                layer.msg('请输入银牌会员价!',{icon:1,time:1000});
                return false;
            }
            var copper_price=$("#copper_price").val();
            if(copper_price==""|| parseFloat(copper_price)<=0){
                layer.msg('请输入铜牌会员价!',{icon:1,time:1000});
                return false;
            }

            var c_id=$("#c_id").val();
            if(c_id==""){
                layer.msg('请选择规格!',{icon:1,time:1000});
                return false;
            }
            var c_name=$("#c_name").val();
            if(c_name==""){
                layer.msg('请输入属性!',{icon:1,time:1000});
                return false;
            }
            var count=$("#count").val();
            if(count==""|| parseInt(count)<=0){
                layer.msg('请输入商品库存!',{icon:1,time:1000});
                return false;
            }
            var goods_num=$("#goods_num").val();
            if(goods_num==""){
                layer.msg('请输入商品货号!',{icon:1,time:1000});
                return false;
            }
            var unit=$("#unit").val();
            if(unit==""){
                layer.msg('请输入单位!',{icon:1,time:1000});
                return false;
            }
            var weigth=$("#weigth").val();
            if(weigth==""){
                layer.msg('请输入重量!',{icon:1,time:1000});
                return false;
            }
            var weigth_unit="千克";




            if($("li.fileImgList").length > 0){
                var goodsImg = [];
                for(var i = 0;i<$("li.fileImgList").length;i++){
                    if (i>=1){
                        goodsImg.push($($("li.fileImgList")[i]).attr("data-url"));
                    }else {
                        $("#main_img").val($($("li.fileImgList")[0]).attr("data-url"))
                    }
                }
                var other_img = "";
                for(var i = 0; i < 5; i++){
                    if(i < goodsImg.length){
                        other_img += "|"+goodsImg[i];
                    }
                }
                if(other_img!=""){
                    other_img=other_img.substring(1,other_img.length);
                }
                $("#other_img").val(other_img)
            }else{
                $("#main_img").val("")
                $("#other_img").val("")
            }

            var main_img=$("#main_img").val();
            if(main_img==""){
                layer.msg('请输入主图!',{icon:1,time:1000});
                return false;
            }
            var other_img=$("#other_img").val();
            if(other_img==""){
                layer.msg('请输入非主图!',{icon:1,time:1000});
                return false;
            }
            var desc=ue.getContent();
            if(desc==""){
                layer.msg('请输入描述!',{icon:1,time:1000});
                return false;
            }
            desc=encodeURIComponent(desc);

            var rec_type_list_str=[];
            $.each($("[name=rec_type]"),function () {
                if(this.checked){
                    rec_type_list_str.push({"rec_type":$(this).val()});
                }
            });

            var sell_platform=$("input[name=sell_platform]:checked").val();
            var status=$("input[name=status]:checked").val();
            var start_quantity=$("#start_quantity").val();

            if(start_quantity==""){
                layer.msg('请输入起订数!',{icon:1,time:1000});
                return false;
            }
            if(parseInt(start_quantity)<=0){
                layer.msg('请输入正确起订数!',{icon:1,time:1000});
                return false;
            }

            var virtual_sales=$("#virtual_sales").val();
            var is_gold=$("#is_gold").prop('checked')?1:0;
            var is_silver=$("#is_silver").prop('checked')?1:0;
            var is_copper=$("#is_copper").prop('checked')?1:0;

            if(is_gold==0 && is_silver==0 && is_copper==0){
                layer.msg('请勾选指定会员!',{icon:1,time:1000});
                return false;
            }


            var express_delivery_template_id=$("#express_delivery_template_id").val();
            if(express_delivery_template_id==""){
                layer.msg('请选择快递模版!',{icon:1,time:1000});
                return false;
            }
            var logistics_template_id=$("#logistics_template_id").val();
            if(logistics_template_id==""){
                layer.msg('请选择物流模版!',{icon:1,time:1000});
                return false;
            }

            var data={};

            var btn_submit= $("#btn_submit");
            btn_submit.attr("disabled",true);
            $.ajax({
                type: 'POST',
                url: "/ajax/t_goods/add",
                data:data,
                //contentType: "application/json;charset=utf-8",
                success: function (d) {
                    if (d.code == 1) {
                        layer.msg('成功',{
                            offset:['50%'],
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        },function(){
                            removeIframe();
                        });
                        return;
                    }else{
                        layer.msg(d.msg,{icon:1,time:1000});
                        $("#btn-add").attr("data-oper", "on");
                    }
                },complete: function(XMLHttpRequest, textStatus) {
                    btn_submit.attr("disabled",false);
                },
                error: function (d) {
                    layer.msg('失败!',{icon:1,time:1000});
                }
            });
        });
    });
</script>
</body>
</html>